/*
 *产品订阅模块
 */

define("func/productSub", [
    "leaflet",
    "func/base",
    "data/ajax",
    "control/panel",
    "control/playback"

], function(L) {

    L.ICT.Func.add("ProductSub", {

        Class: L.Class.extend({

            initialize: function() {
                this._container = this._popPanel = null;
                this._playback = null;
                this.ajax = new L.ICT.Ajax();
                this.dialog = L.ict.app.util.dialog;
                this.ictmap = L.ict.app.ictmap;
                this.menu = L.ict.app.menu;
                this.config = Project_ParamConfig.productSubConfig;
            },

            start: function() {
                if (L.ict.app.ictmap.OperateState.tshf) {
                    return;
                }
                if (this._container || this._popPanel) {
                    return;
                }
                this.alertDepartment(function(departname) {
                    this.departname = departname;
                    this._initUi();
                    this._initEvts();
                    this._container.find(".productNav>li:first-child>a").click();
                });
            },

            stop: function() {
                this.ajax.abort();
                if (this._popPanel) {
                    this._popPanel.remove();
                }
            },

            getContentHtml: function() {
                var html = [];
                html.push('<div class="productSubsContainer">');
                html.push('<ul class="nav productNav">');
                html.push('<li class="active" data-info="productList"><a href="#" >产品列表</a></li>');
                html.push('<li data-info="subscribeList"><a href="#" >订阅列表</a></li>');
                html.push('</ul>');
                html.push('<div class="listDiv"></div>');
                html.push('</div>');
                return html.join("");
            },

            getProductList: function(data) {
                var html = [];
                html.push('<div>');
                html.push('<table>');
                html.push('<thead>');
                html.push('<tr>');
                html.push('<th>区域名称</th>');
                html.push('<th>时间范围</th>');
                html.push('<th>信息模式</th>');
                html.push('<th>操作</th>');
                html.push('<th>状态</th>');
                html.push('</tr>');
                html.push('</thead>');
                html.push('<tbody>');
                for (var i = 0, len = data.length; i < len; i++) {
                    var obj = data[i];
                    var st = L.ict.app.util.dateTime.formatDateH(new Date(obj.stime * 1000));
                    var et = L.ict.app.util.dateTime.formatDateH(new Date(obj.etime * 1000));
                    var mode = obj.mode === 0 ? '融合模式' : '原始模式';
                    var rectstr = { ldlon: obj.ldlon / 600000, ldlat: obj.ldlat / 600000, rulon: obj.rulon / 600000, rulat: obj.rulat / 600000 };
                    var state = this.getProState(obj.state);
                    html.push('<tr>');
                    html.push('<td>' + obj.name + '<button class="locatBtn" data-ldlon="' + rectstr.ldlon + '" data-ldlat="' + rectstr.ldlat + '" data-rulon="' + rectstr.rulon + '" data-rulat="' + rectstr.rulat + '" type="button"></button></td>');
                    html.push('<td>' + st + '——' + et + '</td>');
                    html.push('<td>' + mode + '</td>');
                    if(obj.state === -1 || obj.state === 2){
                        html.push('<td><button class="subBtn active" type="button" data-id="' + obj.id + '" data-info="0">订阅</button></td>');
                    } else {
                        html.push('<td><button class="subBtn" type="button" data-id="' + obj.id + '" data-info="1">订阅</button></td>');
                    }
                    html.push('<td>'+ state +'</td>');
                    html.push('</tr>');
                }
                html.push('</tbody>');
                html.push('</table>');
                html.push('</div>');
                return html.join("");
            },

            getProState:function(code){
                var state = code;
                switch (code){
                    case -1:state = '未订阅';break;
                    case 0 :state = '已发送请求';break;
                    case 1 :state= '已订阅';break;
                    case 2:state = '驳回';break;
                    default:;
                }
                return state;
            },

            getSubScribleList: function(data) {
                var html = [];
                html.push('<div>');
                html.push('<table>');
                html.push('<thead>');
                html.push('<tr>');
                html.push('<th>区域名称</th>');
                html.push('<th>时间范围</th>');
                html.push('<th>信息模式</th>');
                html.push('<th>操作</th>');
                html.push('</tr>');
                html.push('</thead>');
                html.push('<tbody>');
                for (var i = 0, len = data.length; i < len; i++) {
                    var obj = data[i];
                    var st = L.ict.app.util.dateTime.formatDateH(new Date(obj.stime * 1000));
                    var et = L.ict.app.util.dateTime.formatDateH(new Date(obj.etime * 1000));
                    var mode = obj.mode === 0 ? '融合模式' : '原始模式';
                    var rectstr = { ldlon: obj.ldlon / 600000, ldlat: obj.ldlat / 600000, rulon: obj.rulon / 600000, rulat: obj.rulat / 600000 };
                    html.push('<tr>');
                    html.push('<td>' + obj.name + '<button class="locatBtn" data-ldlon="' + rectstr.ldlon + '" data-ldlat="' + rectstr.ldlat + '" data-rulon="' + rectstr.rulon + '" data-rulat="' + rectstr.rulat + '" type="button"></button></td>');
                    html.push('<td>' + st + '——' + et + '</td>');
                    html.push('<td>' + mode + '</td>');
                    html.push('<td><button class="viewBtn" data-id="' + obj.id + '" type="button">查看</button></td>');
                    html.push('</tr>');
                }
                html.push('</tbody>');
                html.push('</table>');
                html.push('</div>');
                return html.join("");
            },

            _initUi: function() {
                var options = {
                    title: '产品订阅',
                    width: 910,
                    height: 458,
                    left: 200,
                    top: 200
                };
                var content = this._container = $(this.getContentHtml());
                options.contentHTML = content;
                var pop = new L.ICT.PopPanel(options);
                pop.show();
                this._popPanel = pop;
                this._popPanel.on("popPanelRemove", function() {
                    this._container = this._popPanel = null;
                }, this);
            },

            _initEvts: function() {
                this._container.find("ul>li").on("click", { context: this }, this._changeNavEvt);
            },

            _changeNavEvt: function(e) {
                var self = e.data.context;
                var type = $(this).data("info");
                $(this).addClass("active").siblings().removeClass("active");
                self._container.find(".listDiv").html('');
                if (type === "productList") { //产品列表
                    //请求后台数据
                    var url = self.config.cplb;
                    var data = {idarr:self.departname};
                    self.ajax.post(url, data, true, self, function(res, error) {
                        if (error) {
                            return;
                        }
                        if (type !== "productList") {
                            return;
                        }
                        if (!this._container) {
                            return;
                        }
                        if (res.state !== 1) {
                            return;
                        }
                        //更新表格
                        this._container.find(".listDiv").html(this.getProductList(res.msg.list)).find("div").mCustomScrollbar({ theme: "minimal-dark" });
                        //注册表格事件
                        this._tableEvts();
                    });
                } else if (type === "subscribeList") { //订阅列表
                    var url = self.config.dylb;
                    var data = { unit: self.departname };
                    self.ajax.post(url, data, true, self, function(res, error) {
                        if (error) {
                            return;
                        }
                        if (type !== "subscribeList") {
                            return;
                        }
                        if (!this._container) {
                            return;
                        }
                        if (res.state !== 1) {
                            return;
                        }
                        //更新表格
                        this._container.find(".listDiv").html(this.getSubScribleList(res.msg.list)).find("div").mCustomScrollbar({ theme: "minimal-dark" });
                        //注册表格事件
                        this._tableEvts();
                    });
                }
            },

            _tableEvts: function() {
                var self = this;
                //定位
                self._container.find(".listDiv table .locatBtn").on("click", function(e) {
                    e.preventDefault();
                    var data = {
                        ldlon: $(this).data('ldlon'),
                        ldlat: $(this).data('ldlat'),
                        rulon: $(this).data('rulon'),
                        rulat: $(this).data('rulat')
                    };
                    var southWest = L.latLng(data.ldlat, data.ldlon),
                        northEast = L.latLng(data.rulat, data.rulon),
                        bounds = L.latLngBounds(southWest, northEast);
                    self.ictmap.map.fitBounds(bounds);
                });
                //订阅
                self._container.find(".listDiv table .subBtn").on("click", function(e) {
                    var type = +$(this).data("info");
                    var id = $(this).data("id");
                    if (type === 0) { //未订阅
                        var url = self.config.cpdy;
                        var data = {};
                        data.id = id;
                        data.unit = self.departname;
                        data.username = L.ict.app.user.userName;
                        data.apply_remarks = '';
                        self.ajax.post(url, data, true, self, function(res, error) {
                            if (res && res.state === 1) {
                                $(e.target).removeClass("active");
                                $(e.target).parent().next().html("已发送请求");
                                $(e.target).data("info", "1");
                            }
                        });
                    }
                });
                //查看
                self._container.find(".listDiv table .viewBtn").on("click", function(e) {
                    var id = $(this).data('id');
                    var url = self.config.dyhf;
                    self.ajax.post(url, { id: id }, true, self, function(res, error) {
                        if (error) {
                            console.log('product error');
                            return;
                        }
                        if (res.state !== 1) {
                            L.ict.app.util.dialog.error('错误',res.msg.msg);
                            return;
                        }
                        if (!this._container) {
                            return;
                        }
                        this.playback(res);
                    });
                });
            },

            playback: function(res) {
                this.ictmap.OperateState.tshf = true;
                this.menu.mainmenu.deactivePortInfo();
                this.menu.mainmenu.disableMenu();
                // this.ictmap.realtarget.hideRealTargetLayer();
                if (this._playback) {
                    this._playback.close();
                    this._playback = null;
                }

                var data = res.msg.shipList;
                var beforeZoom = this.ictmap.map.getZoom();
                var beforeCenter = this.ictmap.map.getCenter();
                var self = this;
                this._playback = new L.ICT.Control.PlayBack(data, null, function() {
                    self._playback = null;
                    self.ictmap.OperateState.tshf = false;
                    self.menu.mainmenu.enableMenu();
                    self.ictmap.map.setView(beforeCenter, beforeZoom);
                    // self.ictmap.realtarget.addRealTargetLayer();
                }, null).show(this.ictmap.map);
            },

            alertDepartment: function(callback) {
                if (this._departpop) {
                    this._departpop.remove();
                }
                var contentHTML = [];
                contentHTML.push('<div class="cpdy_departInputContainer">');
                contentHTML.push('<div>单位名称:&nbsp&nbsp<input type="text" class="departname"></div>');
                contentHTML.push('<p class="msg"></p>');
                contentHTML.push('<div class="btnDiv"><button type="button" class="okbtn">确定</button></div>');
                contentHTML.push('</div>');
                contentHTML = contentHTML.join('');
                var options = {
                    title: '订阅信息',
                    width: 300,
                    height: 160,
                    ismodal: true
                };
                var $container = options.contentHTML = $(contentHTML);
                var pop = this._departpop = new L.ICT.PopPanel(options).show().on("popPanelRemove", function() {
                    this._departpop = null;
                }, this);
                var self = this;
                $container.find('.okbtn').on("click", function() {
                    var $msg = $container.find('.msg')
                    var departname = $container.find('.departname').val().trim();
                    $msg.text('');
                    if (departname === '') {
                        $msg.text('请输入单位名！');
                        return;
                    }
                    pop.remove();
                    typeof callback === 'function' ? callback.call(self, departname) : null;
                });
            }
        })

    });

});
